package cn.dnui_zll731.dnuifood_731zlll.controller;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.dnui_zll731.dnuifood_731zlll.Fragment.BaseFragment;
import cn.dnui_zll731.dnuifood_731zlll.Fragment.Fragment1;
import cn.dnui_zll731.dnuifood_731zlll.Fragment.Fragment2;
import cn.dnui_zll731.dnuifood_731zlll.Fragment.Fragment3;
import cn.dnui_zll731.dnuifood_731zlll.Fragment.Fragment4;
import cn.dnui_zll731.dnuifood_731zlll.R;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout mTab_Shouye;    //LinearLayout
    private LinearLayout mTab_Shoucang;
    private LinearLayout mTab_Sousuo;
    private LinearLayout mTab_My;

    private ImageButton mImg_shouye;     //ImageButton
    private ImageButton mImg_Shoucang;
    private ImageButton mImg_Sousuo;
    private ImageButton mImg_My;

    private Fragment fragment1;          //Fragment5
    private Fragment fragment2;
    private Fragment fragment3;
    private Fragment fragment4;

    private TextView shouye;             //TextView
    private TextView shoucang;
    private TextView sousuo;
    private TextView my;

    private List<BaseFragment> fragmentList;
    private ViewPager viewPager;
    private FragmentPagerAdapter fragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();//初始化控件
        initEvents();//初始化事件

        resetImgs();
        mImg_shouye.setImageResource(android.R.drawable.btn_star_big_on);//初始化首页高亮
        shouye.setTextColor(Color.parseColor("#1296db"));
        setSelect(0);
    }

    // --.protected void onStart() 该方法在 onCreate() 方法之后被调用，
    // --.或者在 Activity 从 Stop 状态转换为 Active 状态时被调用，一般执行了onStart()后就执行onResume()。
    // --.protected void onResume() 在 Activity 从 Pause 状态转换到 Active 状态时被调用。
    protected void onResume(){
        super.onResume();
    }

    private void initView() {
        mTab_Shouye = findViewById(R.id.id_tab_shouye);  //LinearLayout
        mTab_Shoucang = findViewById(R.id.id_tab_shoucang);
        mTab_Sousuo = findViewById(R.id.id_tab_sousuo);
        mTab_My = findViewById(R.id.id_tab_my);

        mImg_shouye = findViewById(R.id.img_home);  //ImageButton
        mImg_Shoucang = findViewById(R.id.img_sc);
        mImg_Sousuo = findViewById(R.id.img_ss);
        mImg_My = findViewById(R.id.img_my);

        shouye = findViewById(R.id.shouye);       //TextView
        shoucang = findViewById(R.id.shoucang);
        sousuo = findViewById(R.id.sousuo);
        my = findViewById(R.id.my);
        fragmentList = new ArrayList<>();
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());

        initViewPager();//初始化viewpager

    }
    private void initViewPager(){
        viewPager = findViewById(R.id.id_content);
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        //设置viewpager的适配器
        viewPager.setAdapter(fragmentPagerAdapter);
        //设置viewpager的页面切换事件
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                setSelect(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void initEvents() {
        mImg_shouye.setOnClickListener(this);
        mImg_Shoucang.setOnClickListener(this);
        mImg_Sousuo.setOnClickListener(this);
        mImg_My.setOnClickListener(this);


        mTab_Shouye.setOnClickListener(this);
        mTab_Shoucang.setOnClickListener(this);
        mTab_Sousuo.setOnClickListener(this);
        mTab_My.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        resetImgs(); //灰色-----------重置图标全灰色
        switch (view.getId())
        {
            case R.id.img_home:
            case R.id.id_tab_shouye:
                setSelect(0);
                break;
            case R.id.img_sc:
            case R.id.id_tab_shoucang:
                setSelect(1);
                break;
            case R.id.img_ss:
            case R.id.id_tab_sousuo:
                setSelect(2);
                break;
            case R.id.img_my:
            case R.id.id_tab_my:
                setSelect(3);
                break;
            default:
                break;
        }
    }


    private void setSelect(int i){
        resetImgs();
        switch (i){
            case 0:
                shouye.setTextColor(Color.parseColor("#1296db"));
                mImg_shouye.setImageResource(android.R.drawable.btn_star_big_on);
                break;
            case 1:
                shoucang.setTextColor(Color.parseColor("#1296db"));
                mImg_Shoucang.setImageResource(android.R.drawable.btn_star_big_on);
                break;
            case 2:
                sousuo.setTextColor(Color.parseColor("#1296db"));
                mImg_Sousuo.setImageResource(android.R.drawable.btn_star_big_on);
                break;
            case 3:
                my.setTextColor(Color.parseColor("#1296db"));
                mImg_My.setImageResource(android.R.drawable.btn_star_big_on);
                break;
            default:
                break;
        }
        viewPager.setCurrentItem(i);
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (fragment1 != null){
            transaction.hide(fragment1);
        }
        if (fragment2 != null){
            transaction.hide(fragment2);
        }
        if (fragment3 != null){
            transaction.hide(fragment3);
        }
        if (fragment4 != null){
            transaction.hide(fragment4);
        }
    }


    private void resetImgs(){
        //灰色-----------重置图标全灰色--首页--收藏--搜索--我的
        mImg_shouye.setImageResource(android.R.drawable.btn_star_big_off);
        mImg_Shoucang.setImageResource(android.R.drawable.btn_star_big_off);
        mImg_Sousuo.setImageResource(android.R.drawable.btn_star_big_off);
        mImg_My.setImageResource(android.R.drawable.btn_star_big_off);

        shouye.setTextColor(Color.parseColor("#ffffff"));
        shoucang.setTextColor(Color.parseColor("#ffffff"));
        sousuo.setTextColor(Color.parseColor("#ffffff"));
        my.setTextColor(Color.parseColor("#ffffff"));
    }

}
